<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/11
 * Time: 16:18
 */

namespace app\index\controller;


use think\Controller;
use think\Request;
use app\library\traits\ResponceJson;
use app\api\User;
class Base extends Controller
{
    use ResponceJson;
    /**
     * 用户登录
     */
    public function login(Request $request)
    {
        if ($request->isPost()){

            $check = [
                'username'  =>  $request->post('uname'),
                'password'  =>  $request->post('pwd'),
                'captcha'   =>  $request->post('captcha'),
            ];

            // 实例化验证器
            $validate = new \app\common\validate\Member();

            // 验证
            if (!$validate->check($check)){
                return json($this->jsonErr(10006));
            }

            // 实例化API中的用户类，进行用户登录
            $User = new User();
            $uid = $User->login($request->param('uname'), $request->post('pwd'));

            if($uid > 0) { //登录成功
                return json($this->jsonSuccess());
            }else{
                return json($this->jsonErr(6));
            }

        } else {
            return $this->view->fetch();
        }
    }


    // 用户登出
    public function logout()
    {
        if(is_login()){
            $Member = new Member();
            $Member->logout();
            session('[destroy]');
            $this->success('退出成功！', url('login'));
        } else {
            $this->redirect('login');
        }
    }
}